package cn.gzsf.springboot06.controller;

import cn.gzsf.springboot06.entity.Item;
import cn.gzsf.springboot06.mapper.ItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ItemController {
    @Autowired
    private ItemMapper itemMapper;
    @RequestMapping("/add")
    public String add(Item item){
        itemMapper.insert(item);
        return "新增成功!<a href='/select'>商品列表</a>";
    }

    @RequestMapping("/select")
    public String select(){
        List<Item> items = itemMapper.select();
        String html = "<table border=1  cellspace='0'>";
        html+="<caption>商品列表</caption>";
        html+="<tr><td>id</td><td>名称</td><td>价格</td><td>简介</td><td>操作</td></tr>";
        //添加多行员工信息
        for(Item e:items){
            html+="<tr>";
            html+="<td>"+e.getId()+"</td>";
            html+="<td>"+e.getName()+"</td>";
            html+="<td>"+e.getPrice()+"</td>";
            html+="<td>"+e.getIntro()+"</td>";
            html+="<td><a href='/delete?id="+e.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }
    @RequestMapping("/delete")
    public String delete(int id){
        itemMapper.delete(id);
        return "删除成功!<a href='/select'>商品列表</a>";
    }
    @RequestMapping("/update")
    public String update(Item item){
        itemMapper.update(item);
        return "修改成功!<a href='/select'>商品列表</a>";
    }
}
